﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using Newtonsoft.Json;
using RN.ERP.Web.ModelService;
using System.Web.UI.HtmlControls;
using Fly.Common.Linq;

namespace RN.ERP.Web
{
    public partial class WebSite : System.Web.UI.MasterPage
    {
        public string PageTitle
        {
            get
            {
                return ConfigurationManager.AppSettings["sysTitle"];
            }
        }

        protected void Page_Init(object sender, EventArgs e)
        {
            string script = ConfigurationManager.AppSettings["MasterPageJs"];
            string[] scripts = script.Split(';');
            for (int i = 0; i < scripts.Length; i++)
            {
                HtmlGenericControl scripts1 = new HtmlGenericControl("script");
                scripts1.Attributes.Add("type", "text/javascript");
                scripts1.Attributes.Add("language", "javascript");
                scripts1.Attributes.Add("src", ResolveClientUrl(scripts[i]));
                Page.Header.Controls.AddAt(i + 1, scripts1);
            }
            //smpMap.SiteMapProvider = GetSiteMapProvider();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //InitMenu();
                //GetUserInfoTip();
            }
        }

        protected SessionUserInfo UserInfo
        {
            get
            {
                if (Session[SessionConstant.SESSION_USERINFO] == null)
                {
                    RebuildSessionUserInfo();
                }
                return Session[SessionConstant.SESSION_USERINFO] == null ? null : (SessionUserInfo)Session[SessionConstant.SESSION_USERINFO];
            }
        }

        private string GetSiteMapProvider()
        {
            switch (UserInfo.RoleType.ToLower())
            {
                case "user":
                    return "UserSiteMap";
                case "manager":
                    return "ManagerSiteMap";
                default:
                    return string.Empty;
            }
        }

        /// <summary>
        /// 重构用户Session数据。
        /// </summary>
        private void RebuildSessionUserInfo()
        {
            UtilitiesTool.RebuildSessionUserInfo();
        }
        /// <summary>
        /// 取得用户登录提示信息
        /// </summary>
        //private void GetUserInfoTip()
        //{
        //    try
        //    {
        //        if (UserInfo.RoleType.ToLower() == "user")
        //        {
        //            RadToolTip1.TargetControlID = userInfoTop.ClientID;//加提示
        //            ltrLoginUser.Text = UserInfo.UserName;//用户名
        //            ltrLoginCorpName.Text = UserInfo.Name;//企业名称
        //            Dictionary<string, string> CorpTypeDic = UtilitiesTool.CorpTypeDic;
        //            ltrLoginCorpType.Text = CorpTypeDic[UserInfo.RoleId.ToString()];//用户类型  
        //            ////绑定当前市场
        //            if (string.IsNullOrEmpty(UserInfo.CurrentArea))
        //            {
        //                ltrCurrentMarket.Text = "未添加";
        //            }
        //            else
        //            {
        //                ltrCurrentMarket.Text = UtilitiesTool.GetAreaTitle(UserInfo.CurrentArea);
        //            }
        //        }
        //    }
        //    catch { }
        //}
        
       
        //#region 主导航
        ///// <summary>
        ///// 加一级菜单
        ///// </summary>
        //protected void InitMenu()
        //{
        //    if (UserInfo == null)
        //    {
        //        return;
        //    }

        //    List<AuthorityService.T_Sys_Function> sysFunctions = null;
        //    using (AuthorityService.AuthorityServiceClient client = new AuthorityService.AuthorityServiceClient())
        //    {
        //        sysFunctions = JsonConvert.DeserializeObject<List<AuthorityService.T_Sys_Function>>(client.GetUserMenus(UserInfo.CorpUserGuid, ""));
        //    }
        //    var sysFunction = sysFunctions.Where(f => f.ParenGuid == Guid.Empty).ToList();
        //    foreach (AuthorityService.T_Sys_Function sysFunc in sysFunction)
        //    {
        //        //Rad菜单项
        //        RadPanelItem item = new RadPanelItem();
        //        // item.CssClass = "select";
        //        item.Text = sysFunc.Title;
        //        item.Value = sysFunc.FunctionGuid.ToString();
        //        //item.NavigateUrl = sysFunc.Url;

        //        //子菜单
        //        if (sysFunctions.Where(f => f.ParenGuid == sysFunc.FunctionGuid).Count() > 0)
        //        {
        //            InitSubMenu(item, sysFunc.FunctionGuid, sysFunc.Title, sysFunctions);
        //        }
        //        item.ExpandParentItems();
        //        this.rpbMenu.Items.Add(item);
        //    }
        //}

        ///// <summary>
        ///// 加子菜单模板容器
        ///// </summary>
        ///// <param name="item"></param>
        ///// <param name="FunctionGuid"></param>
        ///// <param name="topMenuName"></param>
        ///// <param name="sysFunctions"></param>
        //private void InitSubMenu(RadPanelItem item, Guid FunctionGuid, string topMenuName, List<AuthorityService.T_Sys_Function> sysFunctions)
        //{
        //    var sysFunction = sysFunctions.Where(f => f.ParenGuid == FunctionGuid).ToList();
        //    foreach (var child in sysFunction)
        //    {
        //        RadPanelItem childItem = new RadPanelItem();
        //        childItem.Text = child.Title;
        //        childItem.NavigateUrl = child.Url;
        //        item.Items.Add(childItem);
        //    }
        //}

        ///// <summary>
        ///// 加子菜单数据
        ///// </summary>
        ///// <param name="sm"></param>
        ///// <param name="node"></param>
        ///// <param name="FunctionGuid"></param>
        ///// <param name="sysFunctions"></param>
        //private static void PopulateSiteMap(RadSiteMap sm, RadSiteMapNode node, Guid FunctionGuid, List<AuthorityService.T_Sys_Function> sysFunctions)
        //{
        //    var sysFunction = sysFunctions.Where(f => f.ParenGuid == FunctionGuid).ToList();
        //    if (node == null)
        //    {
        //        foreach (var child in sysFunction)
        //        {
        //            node = new RadSiteMapNode(child.Title, child.Url);
        //            sm.Nodes.Add(node);
        //            if (sysFunctions.Where(f => f.ParenGuid == child.FunctionGuid).Count() > 0)
        //            {
        //                PopulateSiteMap(sm, node, child.FunctionGuid, sysFunctions);
        //            }
        //        }
        //    }
        //    else
        //    {
        //        foreach (var child in sysFunction)
        //        {
        //            node.Nodes.Add(new RadSiteMapNode(child.Title, child.Url));
        //            if (sysFunctions.Where(f => f.ParenGuid == child.FunctionGuid).Count() > 0)
        //            {
        //                PopulateSiteMap(sm, node, child.FunctionGuid, sysFunctions);
        //            }
        //        }
        //    }
        //}

        //#endregion
    }
}